package com.mediacloud.app.newsmodule.fragment

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.eventTrack.TXReportManager
import com.mediacloud.app.assembly.util.Utility
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.model.component.ComponentItem
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.CatalogItem
import com.mediacloud.app.model.news.kt.TypeX
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.component.ComponentHolder
import com.mediacloud.app.newsmodule.adaptor.component.ComponentManager
import com.mediacloud.app.newsmodule.adaptor.component.IEvent
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.mediacloud.app.newsmodule.utils.share.ShareWrap
import com.tencent.mmkv.MMKV


class InfoFlowAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>(),
    View.OnClickListener {
    var smallTag = true
    var articleItems: MutableList<Any> = ArrayList()
    var catalogItem: CatalogItem? = null
    var margin = context.resources.getDimensionPixelSize(R.dimen.dimen7)
    val manager: GridLayoutManager? = null
    var shareWrap: ShareWrap? = null
    var events: ArrayList<IEvent> = ArrayList()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val holder = when (viewType) {
            0 -> SmallViewHolder(LayoutInflater.from(context).inflate(R.layout.item_info_flow_small, parent, false))
            1 -> BigVideoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_info_flow_big, parent, false))
            else -> ComponentManager.getComponentHolder(viewType, LayoutInflater.from(context).inflate(ComponentManager.getLayout(viewType), parent, false))
        }
        if (holder is IEvent){
            events.add(holder)
        }
        return holder
    }

    fun setItems(articleItems: MutableList<Any>) {
        smallTag = true
        this.articleItems.clear()
        this.articleItems.addAll(articleItems)
        notifyDataSetChanged()
    }

    fun addItems(articleItems: MutableList<Any>) {
        var positionStart = this.articleItems.size
        if (positionStart < 0) positionStart = 0
        this.articleItems.addAll(articleItems)
        notifyItemRangeChanged(positionStart, this.articleItems.size)

    }

    fun addItem(item: ArticleItem) {
        this.articleItems.add(item)
        notifyItemChanged(this.articleItems.size - 1)
    }

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        val item = articleItems[position]

        when (getItemViewType(position)) {

            0 -> {

                item as ArticleItem
                val holder = viewHolder as SmallViewHolder
//                val lp =holder.itemView.layoutParams as GridLayoutManager.LayoutParams
//                lp.marginStart =0
//                lp.marginEnd =0
//
//                if (item.isInfoFlowSmallAtLeft) {
//                    lp.marginStart =margin
//                } else {
//                    lp.marginEnd =margin
//                }
//
//                holder.itemView.layoutParams =lp

                holder.titleTv.text = item.title
                if (MMKV.defaultMMKV().getBoolean("${item.getId()}", false)) {
                    holder.titleTv.setTextColor(Color.parseColor("#666666"))
                } else {
                    holder.titleTv.setTextColor(Color.parseColor("#222222"))
                }
                holder.logoIv.loadX(item.logo, 2, 1, true, null)
                when (item.type) {
                    TypeX.NEWS.VIDEO -> {
                        holder.videoTag.visibility = View.VISIBLE
                        holder.durationTv.visibility = View.VISIBLE
                        holder.durationTv.text = item.prop4
                    }
                    TypeX.NEWS.LIVE_VIDEO -> {
                        holder.videoTag.visibility = View.VISIBLE
                        holder.durationTv.visibility = View.GONE
                    }
                    else -> {
                        holder.videoTag.visibility = View.GONE
                        holder.durationTv.visibility = View.GONE
                    }
                }
                viewHolder.itemView.tag = position
                viewHolder.itemView.setOnClickListener(this)

            }
            1 -> {
                item as ArticleItem
                val holder = viewHolder as BigVideoViewHolder
                holder.disableNotifyEvent = true
                holder.setInfo(item,position)
                viewHolder.itemView.tag = position
                viewHolder.itemView.setOnClickListener(this)
                val layoutParams = holder.outer_view.layoutParams as FrameLayout.LayoutParams
                if (position == 0) {
                    layoutParams.topMargin = Utility.dpToPx(viewHolder.itemView.context, 12f)
                } else {
                    layoutParams.topMargin = Utility.dpToPx(viewHolder.itemView.context, 18f)
                }
                holder.outer_view.layoutParams = layoutParams
                events.add(holder)
            }
            else -> {
                item as ComponentItem
                val holder = viewHolder as ComponentHolder
                holder.catalogItem = catalogItem
                holder.setViewHolderData(item)
            }
        }

    }

    override fun getItemCount(): Int {
        return articleItems.size
    }

    override fun getItemViewType(position: Int): Int {
        val item = articleItems[position]
        return if (item is ComponentItem) {
            item.widget
        } else {
            item as ArticleItem
            item.infoFlowBig
        }

    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val manager = recyclerView.layoutManager

        if (manager is GridLayoutManager) {
            manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    //注意：这个getSpanSize()方法返回值并不是要显示的列数。解释：getSpanSize返回值不是表示控制每行有几列，而是当前这个Item占这一行得几列
                    val type = getItemViewType(position)
                    return if (type == 0) {
                        1
                    } else {
                        2
                    }
                }
            }
        }
    }

    class SmallViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val titleTv: TextView = itemView.findViewById(R.id.titleTv)
        val logoIv: ImageView = itemView.findViewById(R.id.logoIv)
        val durationTv: TextView = itemView.findViewById(R.id.durationTv)
        val videoTag: View = itemView.findViewById(R.id.videoTag)
    }

    override fun onClick(v: View?) {
        val position = v?.tag as Int
        val item = articleItems[position] as ArticleItem
        NewsItemClickUtils.OpenItemNewsHandle(
            context,
            item.type,
            item,
            catalogItem ?: CatalogItem()
        )
        if ("视频" == TXReportManager.getInstance().firstTabInfo.tabName) {
            TXReportManager.getInstance().app_jhapp_video_interact("点击查看",catalogItem?.name, item,position)
        }
    }

    fun destroy() {
        for (event in events) {
            event.destroy()
        }
        events.clear()
    }

}